<script lang="ts" setup>
import type { DropdownMenuItemProps, NotificationItem } from '@croco/framework';

import { computed, ref, watch } from 'vue';
import { useRouter } from 'vue-router';

import { PRO_DOC_URL, PRO_GITHUB_URL } from '@croco/framework/constants';
import { useWatermark } from '@croco/framework/hooks';
import { BookOpenText, CircleHelp, LockKeyhole, LogOut, MdiGithub } from '@croco/framework/icons';
import { BasicLayout, LockScreen, Notification, UserDropdown } from '@croco/framework/layouts';
import { AuthenticationLoginExpiredModal } from '@croco/framework/libraries';
import { preferences, usePreferences } from '@croco/framework/preferences';
import { useUserStore } from '@croco/framework/stores';
import { openWindow } from '@croco/framework/utils';

import { logout } from '#/api';
import { $t } from '#/locales';
import LoginForm from '#/views/_core/authentication/login.vue';

const notifications = ref<NotificationItem[]>([
    {
        avatar: 'https://avatar.vercel.sh/vercel.svg?text=VB',
        date: '3小时前',
        isRead: true,
        message: '描述信息描述信息描述信息',
        title: '收到了 14 份新周报',
    },
    {
        avatar: 'https://avatar.vercel.sh/1',
        date: '刚刚',
        isRead: false,
        message: '描述信息描述信息描述信息',
        title: '朱偏右 回复了你',
    },
    {
        avatar: 'https://avatar.vercel.sh/1',
        date: '2024-01-01',
        isRead: false,
        message: '描述信息描述信息描述信息',
        title: '曲丽丽 评论了你',
    },
    {
        avatar: 'https://avatar.vercel.sh/satori',
        date: '1天前',
        isRead: false,
        message: '描述信息描述信息描述信息',
        title: '代办提醒',
    },
]);

const router = useRouter();
const userStore = useUserStore();
const { destroyWatermark, updateWatermark } = useWatermark();
const { globalLockScreenShortcutKey, globalLogoutShortcutKey } = usePreferences();
const showDot = computed(() => notifications.value.some((item) => !item.isRead));

const userDropdownRef = ref();
const menus = computed(() => {
    return [
        {
            handler: () => {
                openWindow(PRO_DOC_URL, {
                    target: '_blank',
                });
            },
            icon: BookOpenText,
            value: $t('ui.widgets.document'),
        },
        {
            handler: () => {
                openWindow(PRO_GITHUB_URL, {
                    target: '_blank',
                });
            },
            icon: MdiGithub,
            value: 'GitHub',
        },
        {
            handler: () => {
                openWindow(`${PRO_GITHUB_URL}/issues`, {
                    target: '_blank',
                });
            },
            icon: CircleHelp,
            value: $t('ui.widgets.qa'),
        },
        {
            icon: 'lucide:copyright',
            value: $t('demos.project.about'),
            handler: () => {
                router.push({ name: 'About' });
            },
        },
        {
            separator: true,
        },
        {
            icon: LockKeyhole,
            value: $t('ui.widgets.lockScreen.title'),
            hidden: !preferences.widget.lockScreen,
            enableShortcut: globalLockScreenShortcutKey.value,
            shortcut: 'L',
            separator: true,
            handler: () => {
                userDropdownRef.value.onOpenLock();
            },
        },
        {
            icon: LogOut,
            value: $t('common.logout'),
            enableShortcut: globalLogoutShortcutKey.value,
            shortcut: 'Q',
            handler: () => {
                userDropdownRef.value.onLogout();
            },
        },
    ] as DropdownMenuItemProps[];
});

const avatar = computed(() => {
    return userStore.userInfo?.avatar ?? preferences.app.defaultAvatar;
});

async function handleLogout() {
    await logout();
}

function handleNoticeClear() {
    notifications.value = [];
}

function handleMakeAll() {
    notifications.value.forEach((item) => (item.isRead = true));
}
watch(
    () => preferences.app.watermark,
    async (enable) => {
        if (enable) {
            await updateWatermark({
                content: `${userStore.userInfo?.username} - ${userStore.userInfo?.nickname}`,
            });
        } else {
            destroyWatermark();
        }
    },
    {
        immediate: true,
    },
);
</script>

<template>
    <BasicLayout @clear-preferences-and-logout="handleLogout">
        <template #user-dropdown>
            <UserDropdown ref="userDropdownRef" :avatar :menus :text="userStore.userInfo?.nickname" :description="userStore.userInfo?.email" @logout="handleLogout" />
        </template>
        <template #notification>
            <Notification :dot="showDot" :notifications="notifications" @clear="handleNoticeClear" @make-all="handleMakeAll" />
        </template>
        <template #extra>
            <AuthenticationLoginExpiredModal v-model:open="userStore.loginExpired" :avatar>
                <LoginForm />
            </AuthenticationLoginExpiredModal>
        </template>
        <template #lock-screen>
            <LockScreen :avatar @to-login="handleLogout" />
        </template>
    </BasicLayout>
</template>
